<template>
	<view class="home">
		<navbar></navbar>
		<tab :list="tabList" @tab="tab" :tabIndex="tabIndex"></tab>
		<view class="home-list">
			<list :tab="tabList" @change="change" :activeIndex="activeIndex"></list>
		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				title: 'Hello',
				//本页面请求label数据之后存在tabList里面，然后分发给组件Tab
				tabList: [],
				tabIndex: 0,
				activeIndex: 0
			}
		},
		computed:{
			...mapState(['userinfo'])
		},
		watch:{
			userinfo(newvalue){
				this.getLabel()
			}
		},
		onLoad() {
			uni.$on('labelChange', (res) => {
					this.tabList = []
					this.tabIndex = 0
					this.activeIndex = 0
					this.getLabel()
				})
		},
		methods: {
			getLabel() {
				//console.log(this.$api)
				this.$api.get_label({
					name: 'get_label'
				}).then((res) => {
					const {
						data
					} = res
					// console.log("tab里面的数据",data)
					this.tabList = data
					data.unshift({
						name: '全部'
					})
				})
			},
			tab({
				data,
				index
			}) {
				this.activeIndex = index
			},
			change(current) {
				// console.log("current is",current)
				this.tabIndex = current
				this.activeIndex = current

			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: hidden;

		.home-list {
			flex: 1;
			box-sizing: border-box;
		}

	}
</style>
